/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.Random;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;

/**
 * <p>Project: javase2203 - T16
 * <p>Powered by webrx On 2022-08-10 09:18:15
 * <p>三个到齐，再开饭</p>
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class T16 {
    CyclicBarrier cb = new CyclicBarrier(4, () -> {
        System.out.println("大家到齐了，现在开饭.");
    });

    void eat(){
        Random rand = new Random();
        int time = rand.nextInt(2,30);
        String t = Thread.currentThread().getName();
        System.out.printf("%s开始动身，预计%d秒后到达..%n",t,time);
        try{
            TimeUnit.SECONDS.sleep(time);
            System.out.printf("%s已到达!%n",t);
            cb.await(15,TimeUnit.SECONDS);
        }catch(Exception e){
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        T16 t = new T16();
        new Thread(t::eat,"张三").start();
        new Thread(t::eat,"赵六").start();
        new Thread(t::eat,"张三丰").start();
        Thread wu = new Thread(t::eat,"王五");
        wu.start();
    }
}
